package com.gears_software.sxdc.dccx.ui.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.XingChengListAdapter;
import com.gears_software.sxdc.dccx.data.finder.ICPOrderFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.CPOrderModel;
import com.gears_software.sxdc.dccx.data.model.PagedResultModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp_;
import com.gears_software.sxdc.dccx.ui.activity.TripInformationActivity;
import com.gears_software.sxdc.dccx.ui.activity.TripInformationActivity_;
import com.gears_software.sxdc.dccx.ui.event.UpdateTripListEvent;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.gears_software.sxdc.dccx.ui.util.OttoBus;
import com.github.mzule.activityrouter.annotation.Router;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.orhanobut.logger.Logger;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chezhenzhen on 2017/6/27.
 * 乘客
 */
@EFragment(R.layout.fragment_passenger)
public class PassengerFragment extends Fragment {

    @App
    DCCXApplication app;
    @ViewById
    TextView txt_start;
    @ViewById
    TextView txt_end;
    @ViewById
    TextView txt_date;
    @ViewById
    TextView txt_shuangxiang_icon;
    @ViewById
    TextView txt_date_icon;
    @ViewById
    TextView txt_close_icon;
    @ViewById
    RelativeLayout re_xingcheng;
    @ViewById
    RelativeLayout re_refresh;
    @ViewById
    TextView txt_refresh;
    @ViewById
    TextView txt_nodata_icon;
    @ViewById
    XRecyclerView list_xingcheng;
    @RestService
    ICPOrderFinder icpOrderFinder;
    @Bean
    GearsErrorHandler errorHandler;
    @Pref
    DCCXSp_ sp;
    XingChengListAdapter adapter;
    List<CPOrderModel> list = new ArrayList<CPOrderModel>();
    List<CPOrderModel> moreList = new ArrayList<CPOrderModel>();
    int pageSize = 10;
    int currentPage = 1;
    public String sort = "-departtime";
    String filter = "";
    MaterialDialogProgress progress;

    @AfterViews
    void created() {
        initIconFont();
        icpOrderFinder.setRestErrorHandler(errorHandler);
        isInitRoute();
        if (sp.sortTag().get().equals("") || sp.sortTag().get().equals("时间")) {
            sort = "-departtime";
        } else {
            sort = "price";
        }
        initXRecyclerView();
        progress = new MaterialDialogProgress(getActivity());
        progress.show("正在加载...");
        app.setPassengerRefreshDate(new Date());
        getOrderList();
    }

    public void isInitRoute() {
        if ((!sp.startPoint().get().equals("") && !sp.endPorint().get().equals("")) || !sp.departtime().get().equals("")) {
            re_xingcheng.setVisibility(View.VISIBLE);
            if (sp.startPoint().get().equals("") || sp.endPorint().get().equals("")) {
                txt_shuangxiang_icon.setVisibility(View.GONE);
                txt_start.setVisibility(View.GONE);
                txt_end.setVisibility(View.GONE);
            } else {
                txt_shuangxiang_icon.setVisibility(View.VISIBLE);
                txt_start.setVisibility(View.VISIBLE);
                txt_end.setVisibility(View.VISIBLE);
                txt_start.setText(sp.startPoint().get());
                txt_end.setText(sp.endPorint().get());
            }
            if (sp.departtime().get().equals("")) {
                txt_date_icon.setVisibility(View.GONE);
                txt_date.setVisibility(View.GONE);
            } else {
                txt_date_icon.setVisibility(View.VISIBLE);
                txt_date.setVisibility(View.VISIBLE);
                txt_date.setText(sp.departtime().get());
            }

            filter = "startpoint:" + sp.startPoint().get() + "," + "endpoint:" + sp.endPorint().get() + "," + "departtime:inday:" + sp.departtime().get();
        } else {
            filter = "";
            re_xingcheng.setVisibility(View.GONE);
        }
    }

    void initIconFont() {
        txt_shuangxiang_icon.setTypeface(app.iconfont);
        txt_date_icon.setTypeface(app.iconfont);
        txt_close_icon.setTypeface(app.iconfont);
        txt_nodata_icon.setTypeface(app.iconfont);
    }

    void initXRecyclerView() {

        list_xingcheng.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin);
        list_xingcheng.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        list_xingcheng.setLayoutManager(layoutManager);

        list_xingcheng.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        list_xingcheng.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);


        list_xingcheng.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                app.setPassengerRefreshDate(new Date());
                getOrderList();
                list_xingcheng.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                getMoreOrderList();
                list_xingcheng.loadMoreComplete();

            }
        });
    }

    public void refresh() {
        list_xingcheng.refresh();
    }

    @Background
    public void getOrderList() {
        currentPage = 1;
        PagedResultModel result = icpOrderFinder.getCPOrderList(pageSize, currentPage, filter, sort);
        showResult(result);
    }


    @UiThread
    void showResult(PagedResultModel<CPOrderModel> res) {
        if (res != null) {
            if (res.getErrno() == 0) {
                list = res.getData().getData();
                adapter = new XingChengListAdapter(getActivity(), list);
                list_xingcheng.setAdapter(adapter);
                if (list == null || list.size() == 0) {
                    list_xingcheng.setVisibility(View.GONE);
                    re_refresh.setVisibility(View.VISIBLE);
                } else {
                    list_xingcheng.setVisibility(View.VISIBLE);
                    re_refresh.setVisibility(View.GONE);
                }


            }
        }
        progress.dismiss();

    }

    @Background
    void getMoreOrderList() {
        currentPage++;
        PagedResultModel<CPOrderModel> result = icpOrderFinder.getCPOrderList(pageSize, currentPage, filter, sort);
        showMoreResult(result);
    }

    @UiThread
    void showMoreResult(PagedResultModel<CPOrderModel> res) {
        if (res != null) {
            if (res.getErrno() == 0) {
                moreList = res.getData().getData();
                if (moreList != null && moreList.size() > 0) {
                    for (int i = 0; i < moreList.size(); i++) {
                        list.add(moreList.get(i));
                    }
                    adapter = new XingChengListAdapter(getActivity(), list);
//                    list_xingcheng.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    list_xingcheng.setNoMore(true);

                }
            }
        }
    }

    @Click
    void txt_close_icon() {
        re_xingcheng.setVisibility(View.GONE);
        sp.startPoint().put("");
        sp.endPorint().put("");
        sp.direction().put(0);
        sp.departtime().put("");
        filter = "";
        refresh();
    }

    @Click
    void txt_refresh() {
        refresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        //czzltp用于自动刷新乘客的列表
        long currentSecond = new Date().getTime();
        long refreshSecond = app.getPassengerRefreshDate().getTime();
        int second = (int) ((currentSecond - refreshSecond) / 1000);
        if (second > 30) {
            list_xingcheng.refresh();
        }
    }
}
